//
//  AreaDropDownListView.m
//  PigMarket
//
//  Created by mac on 2019/3/28.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "AreaDropDownListView.h"
#import "AreaDropDownCell.h"

@interface AreaDropDownListView ()<UITableViewDelegate,UITableViewDataSource,UIGestureRecognizerDelegate>
@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic , strong) NSArray *dataArr;
@property (nonatomic, assign) AreaType areaType;
/**
 topSpace
 */
@property (nonatomic , assign) CGFloat topSpace;
@end

static AreaDropDownListView *instance = nil;
@implementation AreaDropDownListView

#pragma mark - init method

+ (instancetype)sharedInstance {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[AreaDropDownListView alloc] init];
    });
    return instance;
}

- (void)showWithFrame:(CGFloat)topSpace type:(AreaType)type areaArr:(NSArray *)areaArr{
    self.topSpace = topSpace;
    self.areaType = type;
    self.dataArr = areaArr;
    
    [[UIApplication sharedApplication].keyWindow addSubview:self];


    
    [self addSubview:self.myTableView];
    [self.myTableView reloadData];
    
    self.frame = CGRectMake(0, self.topSpace, SCREEN_WIDTH, SCREEN_HEIGHT);
    self.myTableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 0);

    self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.0];
    
    [UIView animateWithDuration:0.3 animations:^{
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
        self.myTableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);

    } completion:nil];
    
}
#pragma mark - init method


#pragma mark - delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    AreaDropDownCell *cell = [AreaDropDownCell cellWithTableView:tableView];
    if (self.dataArr.count > 0 && self.dataArr.count > indexPath.row) {
        cell.model = self.dataArr[indexPath.row];
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.selectAreaBlock) {
        SelectTribes *model = self.dataArr[indexPath.row];
        if (self.areaType == AreaTypeTribe) {
           // self.selectAreaBlock(model.tribeName,model.tribeId,self.areaType);
        }else {
           // self.selectAreaBlock(model.regionName,model.parentId,self.areaType);
        }
    }
  
    [self hide];
}

#pragma mark - target action

#pragma mark - private method
- (void)hide {

    [UIView animateWithDuration:0.3 animations:^{
        self.myTableView.frame =  CGRectMake(0, 0, SCREEN_WIDTH, 0);
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.0];
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

#pragma mark - getter and setter
- (UITableView *)myTableView {
    if (!_myTableView) {
        _myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0) style:UITableViewStylePlain];
        _myTableView.rowHeight = 50;
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
        _myTableView.tableFooterView = [[UIView alloc] init];
        //禁止回弹
        _myTableView.bounces = NO;
//        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapHide)];
//        [_myTableView addGestureRecognizer:tap];
    }
    return _myTableView;
}
- (NSArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [[NSArray alloc] init];
    }
    return _dataArr;
}

@end
